package com.whiteape.book.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.whiteape.book.R;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.ui.activity.MainActivity;
import com.whiteape.book.ui.activity.SearchActivity;
import com.whiteape.book.ui.base.BaseRVFragment;
import com.whiteape.book.ui.base.BaseTabFragment;
import com.whiteape.book.ui.guide.SimpleComponent;
import com.whiteape.book.widget.guideview.Guide;
import com.whiteape.book.widget.guideview.GuideBuilder;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class MediaCollectionFragment extends BaseTabFragment {

    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @Override
    protected List<Fragment> createTabFragments() {
        MediaCollectionChildFragment novelFragment = MediaCollectionChildFragment.newInstance(MediaType.MEDIA_TYPE_NOVEL);
        MediaCollectionChildFragment comicFragment = MediaCollectionChildFragment.newInstance(MediaType.MEDIA_TYPE_COMIC);
//        MediaCollectionChildFragment videoFragment = MediaCollectionChildFragment.newInstance(MediaType.MEDIA_TYPE_VIDEO);
        return Arrays.asList(novelFragment, comicFragment/*, videoFragment*/);
    }

    @Override
    protected List<String> createTabTitles() {
        return Arrays.asList(getString(R.string.collection_novel),
                getString(R.string.collection_comic)//,
//                getString(R.string.collection_video)
        );
    }

    @Override
    protected int getContentId() {
        return R.layout.fragment_collection;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        if (getContext() == null) {
            return;
        }
//        showGuideView();

    }

    private void showGuideView() {

            GuideBuilder builder = new GuideBuilder();
            builder.setTargetView(ivSearch)
                    .setAlpha(150)
                    .setHighTargetCorner(20)
                    .setHighTargetPadding(10);
            builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
                @Override
                public void onShown() {
                }

                @Override
                public void onDismiss() {
//                    showGuideView2();
                }
            });

            builder.addComponent(new SimpleComponent());
            Guide guide = builder.createGuide();
            guide.show(getActivity());
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (!hidden) {
//            showGuideView();
//        }
//    }

    @Override
    protected void initEvent() {
        super.initEvent();

        ivSearch.setOnClickListener(v -> startActivity(new Intent(getContext(), SearchActivity.class)));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Fragment fragment = getCurrentFragment();
        if (fragment instanceof BaseRVFragment) {
            if (((BaseRVFragment) fragment).onKeyDown(keyCode, event)) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
